/*
 * Created by Asnull.
 * Website:https://lipux.cn/
 * Date:2023-1-28
 */

// 依赖模块
const  multer = require('multer');
const express = require('express');
const router = express.Router();
const config = require('../config');

// 导入路由处理函数
const  manageFile = require('../handle/manageFile');

// 定制化文件上传需求
const storage = multer.diskStorage({
    destination(req, file, cb) {
        cb(null, './tempFile')
    },
    filename(req, file, cb) {
        cb(null, Date.now() + file.originalname)
    }
})
const upload = multer({
    storage,
    fileFilter(req, file, cb){
        // 文件类型过滤
        if(config.allowMimetype.includes(file.mimetype)){
            cb(null, true)
        } else {
            cb(null, false)
        }
    },
    limits:{
        //限制文件大小
        fileSize: config.allFileSize * 1024 * 1024,
    }
})

//--注册路由--
// 上传文件
router.post('/upload',upload.single('file'),manageFile.errUploadFile,manageFile.uploadFile);

module.exports = router;


